﻿#region license
// CopyRight (c) 2009-2010 HangJing Teconology. All Rights Reserved.
// Function :礼品信息列表
// Created by tuhui at 2010-6-24 16:28:03.
// E-Mail: tuhui@ihangjing.com
#endregion
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.SQLServerDAL;
using Hangjing.Model;
using Hangjing.Common;
using Hangjing.Cache;
using System.Collections.Generic;

using org.in2bits.MyXls;
using Hangjing.WebCommon; //生成excel时需要使用的引用

/// <summary>
/// 配送员操作日志
/// </summary>
public partial class qy_54tss_Admin_Deliveroploglist : System.Web.UI.Page
{
    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? string.Empty : o.ToString();
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }

    deliverlog dal = new deliverlog();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitSqlwhere();
            BindData();


        }
    }

    private void BindData()
    {
        this.AspNetPager1.RecordCount = dal.GetCount(SqlWhere);
        this.rtpGifts.DataSource = dal.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SqlWhere, "lid", 1);
        this.rtpGifts.DataBind();

    }

    /// <summary>
    /// 搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btSearch_Click(object sender, EventArgs e)
    {
        InitSqlwhere();
        BindData();
    }

    /// <summary>
    /// 时间变化搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void settime_Click(object sender, EventArgs e)
    {
        InitSqlwhere();

        BindData();
    }

    /// <summary>
    /// 生成查询条件
    /// </summary>
    protected void InitSqlwhere()
    {
        SqlWhere = " revevar2 = '" + TableENUM.Deliver.ToString() + "'";
        SqlWhere = new RightsTool().getDdeliverlogWhere(SqlWhere);
        if (this.tbStartTime.Text != "")
        {
            SqlWhere = SqlWhere + " and  [optime] > '" + WebUtility.InputText(this.tbStartTime.Text) + "' ";
        }
        if (this.tbEndTime.Text != "")
        {
            SqlWhere = SqlWhere + " and optime < '" + WebUtility.InputText(this.tbEndTime.Text) + " 23:59:59'";
        }
        if (tbKeyword.Text != "")
        {
            SqlWhere += " and  revevar1 like '%" + WebUtility.InputText(tbKeyword.Text) + "%'";
        }

    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }

    protected void DelList_Click(object sender, EventArgs e)
    {
        string IdList = this.hidDels.Value;
        if (dal.DelList(IdList) > 0)
        {
            AlertScript.RegScript(this.Page, "alert('删除成功','success','true',5);init();");
            BindData();
        }
        else
        {
            AlertScript.RegScript(this.Page, "alert('删除失败','error','true',5);init();");
        }
    }

    protected void rtpGifts_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            if (dal.Deldeliverlog(Convert.ToInt32(e.CommandArgument)) > 0)
            {
                AlertScript.RegScript(this.Page, "alert('删除成功','success','true',5);init();");
                BindData();
            }
            else
            {
                AlertScript.RegScript(this.Page, "alert('删除失败','error','true',5);init();");
            }
        }
    }


    /// <summary>
    /// 导出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btExport_Click(object sender, EventArgs e)
    {
        org.in2bits.MyXls.XlsDocument doc = new XlsDocument();

        int allcount = dal.GetCount(SqlWhere);
        int pagegize = 30000;
        int pagecount = allcount / pagegize;
        if (allcount % pagegize != 0)
        {
            pagecount += 1;
        }

        doc.FileName = "配送员操作日志.xls";
        doc.SummaryInformation.Author = "IHangjing"; //作者
        doc.SummaryInformation.Subject = "Order Record";
        doc.DocumentSummaryInformation.Company = "IHangjing.com";

        doc.Workbook.ProtectContents = true;
        string sheetName = "page";

        IList<deliverlogInfo> alllist = dal.GetList(allcount, 1, SqlWhere, "lid", 1);
        for (int xx = 1; xx <= pagecount; xx++)
        {
            IList<deliverlogInfo> list = new List<deliverlogInfo>();
            for (int i = pagegize * (xx - 1); i < pagegize * xx && i < allcount; i++)
            {
                var item = alllist[i];
                list.Add(alllist[i]);
            }

            sheetName = "page" + xx;
            Worksheet sheet = doc.Workbook.Worksheets.Add(sheetName);
            Cells cells = sheet.Cells;

            int rowMin = 1;
            int colMin = 1;
            int colCount = 7;
            string cellName = "";  //列名
            object cellValue = ""; //列的值

            //设置列的格式
            ColumnInfo colInfo = new ColumnInfo(doc, sheet);//生成列格式对象
            colInfo.ColumnIndexStart = 0;
            colInfo.ColumnIndexEnd = 7;
            colInfo.Width = 5 * 1024;          //列的宽度计量单位为 1/256 字符宽
            sheet.AddColumnInfo(colInfo);

            for (int row = 0; row < list.Count + 1; row++)
            {
                if (row == 0)
                {
                    for (int col = 1; col <= colCount; col++)
                    {

                        switch (col)
                        {
                            case 1: cellName = "姓名"; break;
                            case 2: cellName = "事件"; break;
                            case 3: cellName = "数据"; break;
                            case 4: cellName = "旧值"; break;
                            case 5: cellName = "新值"; break;
                            case 6: cellName = "时间"; break;
                            case 7: cellName = "管理员"; break;


                        }
                        org.in2bits.MyXls.Cell cell = cells.Add(rowMin + row, colMin + col - 1, cellName);//列名

                        cell.TopLineStyle = 2;
                        cell.TopLineColor = Colors.Black;
                        cell.BottomLineStyle = 2;
                        cell.BottomLineColor = Colors.Black;
                        if (col == 1)
                        {
                            cell.LeftLineStyle = 2;
                            cell.LeftLineColor = Colors.Black;
                        }
                        cell.RightLineStyle = 2;
                        cell.RightLineColor = Colors.Black;

                        cell.Font.Weight = FontWeight.Bold;
                        cell.Pattern = 1;
                        cell.PatternColor = Colors.Silver;

                    }
                }
                else
                {
                    for (int col = 1; col <= colCount; col++)
                    {

                        switch (col)
                        {
                            case 1: cellValue = list[row - 1].revevar1; break;
                            case 2: cellValue = list[row - 1].eventname; break;
                            case 3: cellValue = list[row - 1].columntext.ToString(); break;
                            case 4: cellValue = list[row - 1].oldvalue; break;
                            case 5: cellValue = list[row - 1].newvalue; break;
                            case 6: cellValue = list[row - 1].optime.ToString(); break;
                            case 7: cellValue = list[row - 1].admin; break;

                        }

                        org.in2bits.MyXls.Cell cell = cells.Add(rowMin + row, colMin + col - 1, cellValue);
                    }
                }

            }
        }

        doc.Send();     //将文档发送到浏览器
        Response.Flush();
        Response.End();

    }

}
